package com.zkca.xf.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.trello.rxlifecycle2.components.support.RxFragment;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.BackgroundToForegroundTransformer;
import com.zkca.xf.ConstantsXF;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.model.javabean.NewsBeab;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.model.javabean.uploadbean.RequestPage;
import com.zkca.xf.presenter.NewsPresenter;
import com.zkca.xf.ui.activity.news.NewsActivity;
import com.zkca.xf.ui.adapter.SingleAdapter;
import com.zkca.xf.ui.adapter.ViewHolder;
import com.zkca.xf.ui.loader.GlideImageLoader;
import com.zkca.xf.utils.DisplayUtil;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.SpaceItemDecoration;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.NewsView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by pc on 2018/8/6.
 * 新闻
 */
public class NewsFragment extends RxFragment implements NewsView {
    private View newsView;
    protected Context mContext;

    private List<String> images;
    private List<String> list_title;
    private XRecyclerView xrv_news;
    private LinearLayoutManager linearLayoutManager;
    private SingleAdapter<NewsBeab.DataBean> adapter;
    private List<NewsBeab.DataBean> dataNewList;
    private NewsPresenter newsPresenter;
    private Map<String, String> map;
    private int number = 0;
    private RequestPage requestPage;
    int pageNum = 1;
    int pageSize = 10;

    private ImageView wushuju;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        newsView = inflater.inflate(R.layout.fragment_news, container, false);
        newsPresenter = new NewsPresenter(this, this);
        images = new ArrayList();
        list_title = new ArrayList<>();
        dataNewList = new ArrayList<>();

        initView();
        initdata(pageNum);
        return newsView;
    }

    private void initdata(int pagenum) {
        Map map = new HashMap<String, Object>();

        requestPage = new RequestPage();
        requestPage.setAccessId(SPUtil.getString(getContext(), Constants_SP.ACCESSID));
        requestPage.setPageNum(pagenum);
        requestPage.setPageSize(pageSize);
        requestPage.setParam(map);
        requestPage.setTimeStamp(TimeUtils.getTime());
        requestPage.setSign(MyApplication.getInstance().generateSign(requestPage));
        newsPresenter.News(requestPage);
    }


    private void initView() {
        xrv_news = (XRecyclerView) newsView.findViewById(R.id.xrv_news);


        wushuju=newsView.findViewById(R.id.wushuju);
        linearLayoutManager = new LinearLayoutManager(mContext);
        xrv_news.setLayoutManager(linearLayoutManager);
        xrv_news.addItemDecoration(new SpaceItemDecoration(DisplayUtil.dip2px(
                mContext, 0.5f)));


        xrv_news.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                xrv_news.setLoadingMoreEnabled(true);
                dataNewList.clear();
                pageNum=1;
               initdata (pageNum);
            }

            @Override
            public void onLoadMore() {
                pageNum++;
                initdata(pageNum);
            }
        });

        adapter = new SingleAdapter<NewsBeab.DataBean>(mContext, dataNewList, R.layout.listnews_item) {
            @Override
            public void convert(ViewHolder holder, final NewsBeab.DataBean newsBean, final int position) {
                TextView tv_title = holder.getView(R.id.tv_title);
                TextView tv_data = holder.getView(R.id.tv_data);
                TextView text_eye = holder.getView(R.id.text_eye);
                ImageView iv_news = holder.getView(R.id.iv_news);

                long sendTime = newsBean.getSendTime();
                String formatDate2 = TimeUtils.getFormatDate2(sendTime);
                tv_title.setText(newsBean.getMsgContext());
                tv_data.setText(formatDate2);
                text_eye.setText(newsBean.getMsgCode());
                LogUtil.e("缩略图地址：",ConstantsXF.URL_SERVER_CE + newsBean.getMsgExtno());

                Glide.with(mContext).load(ConstantsXF.URL_SERVER_CE + newsBean.getMsgExtno()).asBitmap().placeholder(R.mipmap.zwf).error(R.mipmap.notice).into(iv_news);
                holder.setOnClickListener(R.id.ll_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, NewsActivity.class);
                        intent.putExtra("URL", newsBean.getRtnCfrom());
                        mContext.startActivity(intent);
                    }
                });
            }
        };
        xrv_news.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getNews(String s) {
        LogUtil.e("121", s);
        Gson gson = new Gson();
        NewsBeab friealarmLbean = gson.fromJson(s, NewsBeab.class);
        try {
            xrv_news.refreshComplete();
            xrv_news.loadMoreComplete();
            if (friealarmLbean.getResult() == 0) {
                List<NewsBeab.DataBean> data = friealarmLbean.getData();
                if (data != null && data.size() > 0) {
                    dataNewList.addAll(data);
                }
                if (friealarmLbean.getTotalpage() == pageNum) {
                    xrv_news.setLoadingMoreEnabled(false);
                } else {
                    xrv_news.setLoadingMoreEnabled(true);
                }
                if (pageNum == 1 && data.size() == 0) {
                    wushuju.setVisibility(View.VISIBLE);
                } else {
                    wushuju.setVisibility(View.GONE);
                }
                adapter.notifyDataSetChanged();
            }else {
                wushuju.setVisibility(View.VISIBLE);
                ToastUtils.show(getContext(),friealarmLbean.getInfo());
            }
        } catch (Exception e) {

        }
    }
}
